﻿using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Updating;
using DevExpress.Xpo;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.BaseImpl;
using Microsoft.Extensions.DependencyInjection;
using CustomLogicInsteadOfDelete.Module.BusinessObjects;

namespace CustomLogicInsteadOfDelete.Module.DatabaseUpdate;

// For more typical usage scenarios, be sure to check out https://docs.devexpress.com/eXpressAppFramework/DevExpress.ExpressApp.Updating.ModuleUpdater
public class Updater : ModuleUpdater {
  public Updater(IObjectSpace objectSpace, Version currentDBVersion) :
      base(objectSpace, currentDBVersion) {
  }
  public override void UpdateDatabaseAfterUpdateSchema() {
    base.UpdateDatabaseAfterUpdateSchema();
    string name = "MyName";
    TStudentBo theObject = ObjectSpace.FirstOrDefault<TStudentBo>(u => u.Name == name);
    if (theObject == null) {
      theObject = ObjectSpace.CreateObject<TStudentBo>();
      theObject.Name = name;
      theObject.Age = 30;
    }

    ObjectSpace.CommitChanges(); //Uncomment this line to persist created object(s).
  }
  public override void UpdateDatabaseBeforeUpdateSchema() {
    base.UpdateDatabaseBeforeUpdateSchema();
    //if(CurrentDBVersion < new Version("1.1.0.0") && CurrentDBVersion > new Version("0.0.0.0")) {
    //    RenameColumn("TStudentBoTable", "OldColumnName", "NewColumnName");
    //}
  }
}
